<?php
defined ( 'SYSPATH' ) or die ( 'No direct script access.' );
/********************************************************************************
 * breakphp	=>	框架核心
 * @copyright 	2012-2013 http://www.breakphp.com,All rights reserved. 			*
 * @link 		http://www.breakphp.com                     				    *
 * @author 		BREAK(306309883@qq.com) 				                        *
 * @date 		2012-07-31			                                            * 
 *******************************************************************************/
class Breakphp_Core {
	
	public static $base_url = '/';
	public static $charset = 'utf-8';
	protected static $_init = FALSE;
	protected static $_paths = array (APPPATH, SYSPATH );
	protected static $_files = array ();
	protected static $_modules = array ();
	
	public static function init(array $settings = NULL) {
		if (Breakphp::$_init) {
			//保证单例
			return;
		}
		
		// 标记已初始化
		Breakphp::$_init = TRUE;
		
		if (isset ( $settings ['base_url'] )) {
			// Set the base URL
			Breakphp::$base_url = rtrim ( $settings ['base_url'], '/' ) . '/';
		}
		
		if (isset ( $settings ['charset'] )) {
			Breakphp::$charset = strtolower ( $settings ['charset'] );
		}
		
		// 预处理get、post、cookie数据
		$_GET = Breakphp::sanitize ( $_GET );
		$_POST = Breakphp::sanitize ( $_POST );
		$_COOKIE = Breakphp::sanitize ( $_COOKIE );
	}
	
	public static function sanitize($value) {
		if (is_array ( $value ) or is_object ( $value )) {
			foreach ( $value as $key => $val ) {
				$value [$key] = Breakphp::sanitize ( $val );
			}
		} elseif (is_string ( $value )) {
			if (Breakphp::$magic_quotes === TRUE) {
				$value = stripslashes ( $value );
			}
			
			if (strpos ( $value, "\r" ) !== FALSE) {
				$value = str_replace ( array ("\r\n", "\r" ), "\n", $value );
			}
		}
		
		return $value;
	}
	
	public static function auto_load($class) {
		try {
			// 类名转路径
			$file = str_replace ( '_', '/', strtolower ( $class ) );
			
			if ($path = Breakphp::find_file ( 'lib', $file )) {
				// 载入
				require $path;
				
				return TRUE;
			}
			
			//文件不存在
			return FALSE;
		} catch ( Exception $e ) {
			Breakphp_Exception::handler ( $e );
			die ();
		}
	}
	
	public static function find_file($dir, $file, $ext = NULL, $array = FALSE) {
		if ($ext === NULL) {
			$ext = EXT;
		} elseif ($ext) {
			$ext = ".{$ext}";
		} else {
			$ext = '';
		}
		
		// 拼接文件路径
		$path = $dir . DIRECTORY_SEPARATOR . $file . '.class.' . $ext;
		
		if ($array or $dir === 'config') {
			$paths = array_reverse ( Breakphp::$_paths );
			
			$found = array ();
			
			foreach ( $paths as $dir ) {
				if (is_file ( $dir . $path )) {
					$found [] = $dir . $path;
				}
			}
		} else {
			$found = FALSE;
			
			foreach ( Breakphp::$_paths as $dir ) {
				if (is_file ( $dir . $path )) {
					$found = $dir . $path;
					break;
				}
			}
		}
		
		return $found;
	}
	
	public static function include_paths() {
		return Breakphp::$_paths;
	}
	
	public static function load($file) {
		return include $file;
	}

} // End Breakphp
